//
//  XMMineViewController.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/9.
//

#import "XMMineViewController.h"
#import "XMMineView.h"
#import "XMNetwork+mine.h"
#import "XMAddFriendViewController.h"
#import "XMSetViewController.h"
#import "XMHomePageEditViewController.h"
#import "XMProductModel.h"
#import "XMWithdrawDetailViewController.h"
#import "XMWithdrawViewController.h"
#import "XMLatelyVisitorsViewController.h"
#import "XMMyMeetingViewController.h"
#import "XMMeetBoxViewController.h"
#import "XMMyOrderViewController.h"
#import "XMMyBrowsHistoryViewController.h"
#import "XMRealNameAuthenViewController.h"
#import "XMQRCodeViewController.h"
#import "XMScanCodeViewController.h"
#import "XMPicwordDetailViewController.h"
#import "XMMeetDetailViewController.h"

#import "XMLoginOtherPhoneController.h"

@interface XMMineViewController ()<XMMineViewDelegate>

@property (nonatomic, strong) XMMineView *mineView;

@property (nonatomic, strong) XMUserInfoModel *userInfoModel;

@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) NSInteger listType;

@end

@implementation XMMineViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self hideNavigationBar:YES animated:YES];
    [self getUserInfo];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatUI];
}

- (void)creatUI {
    [self.view addSubview:self.mineView];
//    [self getUserInfo];
//    [self login];
    _page = 1;
    [self getMyProducts];
    _listType = 0;
}

//- (void)login {//NTY6MTE1MA==
//    XMLoginOtherPhoneController *loginVC = [[XMLoginOtherPhoneController alloc] init];
//    XMBaseNavigationController *loginNVC = [[XMBaseNavigationController alloc] initWithRootViewController:loginVC];
//    loginNVC.modalPresentationStyle = UIModalPresentationFullScreen;
//    [self presentViewController:loginNVC animated:YES completion:nil];
//}

- (void)getUserInfo {
    XMWeakSelf;
    [_Network getHomePageInfoSuccess:^(id _Nonnull responseObject) {
        XMUserInfoModel *userInfoModel = [XMUserInfoModel yy_modelWithJSON:[responseObject objectForKey:@"data"]];
        [weakSelf.mineView setContentWithUserInfoModel:userInfoModel];
        weakSelf.userInfoModel = userInfoModel;
    } failure:^(NSError * _Nonnull error) {
        
    }];
//    [_Network getUserInfoSuccess:^(id  _Nonnull responseObject) {
//        XMUserInfoModel *userInfModel = [XMUserInfoModel yy_modelWithJSON:[responseObject objectForKey:@"data"]];
//        [weakSelf.mineView setContentWithUserInfoModel:userInfModel];
//    } failure:^(NSError * _Nonnull error) {
//
//    }];
}

//我的作品
- (void)getMyProducts {
    XMWeakSelf;
    _page = 1;
    [_Network getMyProductsSuccess:^(id  _Nonnull responseObject) {
        NSArray *productModelArr = [NSArray yy_modelArrayWithClass:[XMProductModel class] json:responseObject[@"data"]];
        [weakSelf.mineView setProductListWithProductModelArr:productModelArr
                                                        page:weakSelf.page];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

//我收藏的
- (void)getMyCollect {
    XMWeakSelf;
    [_Network getMyCollectWithPage:[NSString stringWithFormat:@"%ld", _page]
                           success:^(id  _Nonnull responseObject) {
        NSArray *productModelArr = [NSArray yy_modelArrayWithClass:[XMProductModel class] json:responseObject[@"data"]];
        [weakSelf.mineView setProductListWithProductModelArr:productModelArr
                                                        page:weakSelf.page];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

//我喜欢的
- (void)getMyLike {
    XMWeakSelf;
    [_Network getMyLikeWithPage:[NSString stringWithFormat:@"%ld", _page]
                        success:^(id  _Nonnull responseObject) {
        NSArray *productModelArr = [NSArray yy_modelArrayWithClass:[XMProductModel class] json:[responseObject[@"data"] safeArrayForKey:@"records"]];
        [weakSelf.mineView setProductListWithProductModelArr:productModelArr
                                                        page:weakSelf.page];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

#pragma mark - Delegate
//添加朋友
- (void)addFriend {
    XMAddFriendViewController *addFriendVC = [[XMAddFriendViewController alloc] init];
    addFriendVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:addFriendVC animated:YES];
}

//查看二维码
- (void)qrcodeCheckAction {
    XMQRCodeViewController *qrcodeVC = [[XMQRCodeViewController alloc] init];
    qrcodeVC.hidesBottomBarWhenPushed = YES;
    qrcodeVC.userInfoModel = self.userInfoModel;
    [self.navigationController pushViewController:qrcodeVC animated:YES];
}

//扫一扫
- (void)scanAction {
    XMScanCodeViewController *scanCodelVC = [[XMScanCodeViewController alloc] init];
    scanCodelVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:scanCodelVC animated:YES];
}

//最近访客
- (void)checkAccessRecord {
    XMLatelyVisitorsViewController *latelyVisitorsVC = [[XMLatelyVisitorsViewController alloc] init];
    latelyVisitorsVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:latelyVisitorsVC animated:YES];
}

//设置
- (void)setAction {
    XMSetViewController *setVC = [[XMSetViewController alloc] init];
    setVC.userInfoModel = self.userInfoModel;
    setVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:setVC animated:YES];
}

//编辑主页
- (void)homePageEdit {
    XMHomePageEditViewController *homePageEditVC = [[XMHomePageEditViewController alloc] init];
    homePageEditVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:homePageEditVC animated:YES];
}

//实名认证
- (void)authenAction {
    XMRealNameAuthenViewController *realNameAuthenVC = [[XMRealNameAuthenViewController alloc] init];
    realNameAuthenVC.userInfoModel = self.userInfoModel;
    realNameAuthenVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:realNameAuthenVC animated:YES];
}

- (void)checkProductDetailWithProductId:(NSString *)productId {
    if (_listType== 1) {
        XMMeetDetailViewController *meetDetailVC = [[XMMeetDetailViewController alloc] init];
        meetDetailVC.serviceId = productId;
        meetDetailVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:meetDetailVC animated:YES];
    }
    else {
        XMPicwordDetailViewController *picwordDetailVC = [[XMPicwordDetailViewController alloc] init];
        picwordDetailVC.picwordId = productId;
        picwordDetailVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:picwordDetailVC animated:YES];
    }
}

//列表类型选择
- (void)listTypeSelect:(NSInteger)selectIndex {
    _listType = selectIndex;
    [self getList];
}

- (void)refreshData {
    _page = 1;
    [self getList];
}

- (void)loadMoreData {
    _page += 1;
    [self getList];
}

- (void)getList {
    switch (_listType) {
        case 0: {
            [self getMyProducts];
        }
            break;
        case 1: {
            [self getMyCollect];
        }
            break;
        case 2: {
            [self getMyLike];
        }
            break;
            
        default:
            break;
    }
}

//查看详情
- (void)checkEarnDetailAction {
    XMWithdrawDetailViewController *withdrawDetailVC = [[XMWithdrawDetailViewController alloc] init];
    withdrawDetailVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:withdrawDetailVC animated:YES];
}

//提现
- (void)putForwardMoneyAction {
    XMWithdrawViewController *withdrawVC = [[XMWithdrawViewController alloc] init];
    withdrawVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:withdrawVC animated:YES];
}

//查看订单
- (void)checkMyOrderListActionWithOrderType:(NSInteger)orderType {
    switch (orderType) {
        case 0: {
            XMUserModel *userModel = [[XMUserManager shareInstance] getUser];
            XMMyMeetingViewController *myMeetingVC = [[XMMyMeetingViewController alloc] init];
            myMeetingVC.userId =  userModel.userId;
            myMeetingVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:myMeetingVC animated:YES];
        }
            break;
        case 1: {
            XMMeetBoxViewController *meetBoxVC = [[XMMeetBoxViewController alloc] init];
            meetBoxVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:meetBoxVC animated:YES];
        }
            break;
        case 2: {
            XMMyOrderViewController *myOrderVC = [[XMMyOrderViewController alloc] init];
            myOrderVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:myOrderVC animated:YES];
        }
            break;
        case 3: {
            XMMyBrowsHistoryViewController *browsHisVC = [[XMMyBrowsHistoryViewController alloc] init];
            browsHisVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:browsHisVC animated:YES];
        }
            
        default:
            break;
    }
}

#pragma mark - Init
- (XMMineView *)mineView {
    if (!_mineView) {
        _mineView = [[XMMineView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - kTabBarH)];
        _mineView.delegate = self;
    }
    return _mineView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
